//
// Created on 2025/10/16.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef MSLITEVIDEOENHANCE_WAV_HEADER_H
#define MSLITEVIDEOENHANCE_WAV_HEADER_H
#include <cstdint>
#include <fstream>
#include <vector>

// WAV文件头结构体
struct WavHeader {
    // RIFF块
    char chunkId[4] = {'R', 'I', 'F', 'F'};
    uint32_t chunkSize; // 文件总大小 - 8
    char format[4] = {'W', 'A', 'V', 'E'};

    // fmt子块
    char subchunk1Id[4] = {'f', 'm', 't', ' '};
    uint32_t subchunk1Size = 16; // PCM格式为16
    uint16_t audioFormat = 1;    // PCM = 1
    uint16_t numChannels;        // 声道数
    uint32_t sampleRate;         // 采样率
    uint32_t byteRate;           // 每秒字节数
    uint16_t blockAlign;         // 每个样本的字节数
    uint16_t bitsPerSample;      // 每个样本的位数

    // data子块
    char subchunk2Id[4] = {'d', 'a', 't', 'a'};
    uint32_t subchunk2Size; // 音频数据大小
};

// 字节序转换工具（鸿蒙通常是小端序）
inline void writeLittleEndian(std::ofstream &file, uint32_t value) {
    file.put(static_cast<char>(value & 0xFF));
    file.put(static_cast<char>((value >> 8) & 0xFF));
    file.put(static_cast<char>((value >> 16) & 0xFF));
    file.put(static_cast<char>((value >> 24) & 0xFF));
}

inline void writeLittleEndian(std::ofstream &file, uint16_t value) {
    file.put(static_cast<char>(value & 0xFF));
    file.put(static_cast<char>((value >> 8) & 0xFF));
}
#include <string>
#include <vector>

// 在头文件中声明
extern "C" {
/**
 * 保存音频数据为WAV文件
 * @param audioData 音频数据数组
 * @param dataLength 数据长度
 * @param sampleRate 采样率
 * @param numChannels 声道数
 * @param filePath 文件路径
 * @return 0成功，-1失败
 */
int SaveAudioToWav(const float *audioData, int dataLength, int sampleRate, int numChannels, const char *filePath);

/**
 * 保存模型输出为WAV文件
 * @param modelOutput 模型输出数据
 * @param outputLength 输出数据长度
 * @param sampleRate 采样率
 * @param filePath 文件路径
 * @return 0成功，-1失败
 */
int SaveModelOutputToWav(const float *modelOutput, int outputLength, int sampleRate, const char *filePath);
}


#endif // MSLITEVIDEOENHANCE_WAV_HEADER_H
